﻿using B.S.Database.Domain.Entity;
using B.S.Database.ErrorCode;
using B.S.Database.Interface.Interface;
using B.S.Database.Write.Api.Application.Commands.Traceability;
using MediatR;
using System.Collections.Generic;

namespace B.S.Database.Write.Api.Application.CommandHandler.Traceability
{
    public class TraceabilityBatchdeleteCommandHandler : IRequestHandler<TraceabilityBatchdeleteCommands, Apiresult<object>>
    {
        private readonly IBaseResotity<Basic_Traceability> traceabilityRepo;

        public TraceabilityBatchdeleteCommandHandler(IBaseResotity<Basic_Traceability> TraceabilityRepo)
        {
            traceabilityRepo = TraceabilityRepo;
        }
        public Task<Apiresult<object>> Handle(TraceabilityBatchdeleteCommands request, CancellationToken cancellationToken)
        {
            Apiresult<object> res = new Apiresult<object>();
            try
            {
                var entities = traceabilityRepo.GetAll().Where(c => request.Ids.Contains(c.TraceabilityId)).ToList();
                if (entities == null || !entities.Any())
                {
                    res.Code = ApiEnums.失败;
                    res.Message = "未找到要删除的数据";
                    return Task.FromResult(res);
                }
                foreach (var entity in entities)
                {
                    entity.IsDelete = true;
                    traceabilityRepo.Update(entity);
                }
                res.Code = ApiEnums.成功;
                res.Message = "批量逻辑删除成功";
            }
            catch (Exception ex)
            {
                res.Code = ApiEnums.失败;
                res.Message = ex.Message;
            }
            return Task.FromResult(res);
        }
        // 批量逻辑删除
        //public Task<Apiresult<object>> BatchLogicalDeleteAsync(List<int> ids, CancellationToken cancellationToken)
        //{
            
        //}
    }
}
